import {Toast} from 'vant'
import router from '../../router'

const logon = {
    state: {
        WebConfig: '',
        token: localStorage.getItem('adminToken') || ''
    },
    mutations: {
        getWebConfig(state, data) {
            state.WebConfig = data;
        },
        SETTOKEN(state, token) {
            state.token = token;
        },
        GO() {
            router.push('/user')
        }
    },
    actions: {
        getWebConfig(context) {
            this.$http.webConfig().then(res => {
                context.commit('getWebConfig', res.data.data)
            })
        },
        goLogon({commit}, data) {

            this.$http.login(data).then(res => {
                if (res.data.code === 200) {
                    Toast.success('登录成功!')

                    let {code, data: {remember_token: token}} = res.data;
                    localStorage.setItem('adminToken', token)

                    commit('GO')
                }
            })

        },
        smsCode(context, data) {
            this.$http.smsCode(data).then(res => {
                console.log(res)
                if (res.data.data === true) {
                    Toast.success('验证码发送成功!');
                }
            })
        }
    },
    module: {}
};

export default logon;